package com.example.provider.service.impl;

import com.example.common.entity.Ad;
import com.example.common.entity.Message;
import com.example.provider.dao.AdDao;
import com.example.provider.service.AdService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class AdServiceImpl implements AdService {

    @Resource
    private AdDao adDao;

    //申请
    @Transactional
    public Message addAd(Ad ad) {
        if (ad.getRole() == 1)
            adDao.insertAdByCompany(ad);
        else if (ad.getRole() == 2)
            adDao.insertAdByWorkroom(ad);
        return  new Message(800,"成功");
    }

    //上架
    @Transactional
    public Message upAd(Ad ad) {
        adDao.upAd(ad);
        return  new Message(800,"成功");
    }

    //管理员查看
    public List<Ad> findAd() {
        return  adDao.findAd();
    }

    //轮播图查找正上架的广告
    public List<Ad> findAding() {
        return  adDao.findAding();
    }

    //删除
    @Transactional
    public Message deleteAding(Ad ad) {
        adDao.deleteAding(ad);
        return  new Message(800,"成功");
    }

    /**
     * 企业或工作室查看广告申请状态
     * @param ad
     * @return
     */
    public List<Ad> checkAdStatus(Ad ad) {
        List<Ad> ads = new ArrayList<>();
        if (ad.getRole() == 1)
            ads = adDao.searchByCompanyId(ad);
        else if(ad.getRole() == 2)
            ads = adDao.searchByWorkroomId(ad);
        return ads;
    }
}